<?php
class Page extends CActiveRecord
{
    public $id;
    public $type;
    public $key;
    public $title;
    public $content;

    static $TYPE = array(
        'game_help' => 'game_help',
        'coin_help' => 'coin_help'
    );

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'pages';
    }

    public function afterFind(){
        $search = array('{domain}', '{sitename}');
        $replacement = array(
            Yii::app()->params['domain'].Yii::app()->request->baseUrl,
            Yii::app()->name
        );
        $this->content = str_replace($search, $replacement, $this->content);
        parent::afterFind();
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('type',$this->type);
        $criteria->compare('language',$this->language,true);
        $criteria->compare('key',$this->key);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('content',$this->content,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Get help content by key
     * @param string $key Key name for get help
     * @return Help Tiltle and content for help key
     */
    public static function getHelp($key)
    {
        if(empty($key)) return null;
        $help = self::model()->findByAttributes(array('type'=>self::$TYPE['game_help'], 'key'=>$key));
        return is_object($help) ? $help : null;
    }

}
?>